package com.example.cart.controller;

import com.example.cart.model.CartItem;
import com.example.cart.properties.ShoppingCartProperties;
import com.example.cart.service.ShoppingCartService;
import com.example.cc.common.annotation.MyValue;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/cart")
@RequiredArgsConstructor
public class ShoppingCartController {

    private final ShoppingCartService shoppingCartService;
    private final ShoppingCartProperties shoppingCartProperties;

    // 使用自定义注解读取配置
    @MyValue("${shopping.cart.max-capacity}")
    private Integer maxCapacity;

    public Integer getCapacity() {
        return maxCapacity;
    }

    @GetMapping("/getMaxCapacity")
    public Integer getMaxCapacity() {
        return Optional.ofNullable(this.maxCapacity).orElse(-1);
    }

    @PostMapping("/add")
    public void addItem(@RequestBody CartItem item) {
        shoppingCartService.addItem(item);
    }

    @GetMapping("/items")
    public List<CartItem> getItems() {
        return shoppingCartService.getItems();
    }

    @DeleteMapping("/remove/{productId}")
    public void removeItem(@PathVariable String productId) {
        shoppingCartService.removeItem(productId);
    }

    @DeleteMapping("/clear")
    public void clearCart() {
        shoppingCartService.clearCart();
    }
}
